package com.hrt.common.systemmanager.systemsetting.dict.type;

import com.btcode.db.sqlparameter.SQLParameterList;
import com.btcode.exception.BusinessException;
import com.btcode.orm.model.IDomainField;
import com.btcode.orm.model.IField;
import com.btcode.orm.model.IModelAddition;
import com.btcode.orm.relation.ex.IRelation;
import com.btcode.orm.relation.ex.Relation;
import com.hrt.common.db.CommonDBManager;
import com.hrt.common.systemmanager.systemsetting.dict.data.BcDictDataModel;
import java.util.List;

public class BcDictTypeModelAddition implements IModelAddition<BcDictTypeModel> {

    @Override
    public void generateDefaultValue(BcDictTypeModel model) {
        // TODO Auto-generated method stub

    }

    @Override
    public void initField(List<IField> fields) {
        // TODO Auto-generated method stub

    }

    @Override
    public void initRelations(List<IRelation> relations) {
        IRelation relation = new Relation(BcDictTypeModel.code, BcDictDataModel.dictTypeCode, true,
                                          false);
        relations.add(relation);
    }

    @Override
    public void initDomainField(List<IDomainField> domainFields) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeCreate(BcDictTypeModel model) {

        if (model.getCode() == null) {
            throw new BusinessException("字典编码不能为空");
        }

        model.setCode(model.getCode().toUpperCase());

        String sql = " SELECT * FROM bc_dict_type where CODE = @CODE ";
        SQLParameterList pc = new SQLParameterList();
        pc.put("CODE", model.getCode());

        List<BcDictTypeModel> models = CommonDBManager.getORMExecuter().getModelListSQL(sql, pc,
                                                                                        BcDictTypeModel.class);

        if (models.size() > 0) {
            throw new BusinessException("已存在相同的字典类型。");
        }
    }

    @Override
    public void afterCreate(BcDictTypeModel model) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeUpdate(BcDictTypeModel model) {
        model.setCode(null);
    }

    @Override
    public void afterUpdate(BcDictTypeModel model) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeDelete(BcDictTypeModel model) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterDelete(BcDictTypeModel model) {
        // TODO Auto-generated method stub

    }

}
